package com.smh.a03UDPDemo2;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class ReceiveMessageDemo {
    public static void main(String[] args) throws IOException {

        DatagramSocket ds = new DatagramSocket(10086);
        byte[] bytes = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);

        while (true) {
            ds.receive(dp);

            String address = dp.getAddress().getHostAddress();
            String hostName = dp.getAddress().getHostName();
            byte[] data = dp.getData();
            int port = dp.getPort();
            int length = dp.getLength();


            System.out.println("接收到的数据" + new String(data, 0, length));
            System.out.println("该数据是从" + address + "这台电脑中的" + port + "端口发送的");
            System.out.println(hostName);

            System.out.println(new String(bytes));
            if (new String(data).equals("886")) {
                break;
            }
        }

        ds.close();

    }
}
